// userPageA/pages/topupInput/topupInput.js
const app = getApp()
const WXAPI = require("../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '充值', // 导航栏 中间的标题
      textColor: '#2C3241', // 标题颜色
      bgColor: '#FFFFFF', // 导航栏背景颜色
      isColor: true,
    },
    customerCardId: '',
    customerData:{},
    imgHttp: app.globalData.imgHttp,
    money: '',
    countdown: 60,
    orderId: '',
    show:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var customerCardId = options.customerCardId
    this.setData({
      customerCardId:customerCardId
    })
    this.getDetail()
  },
  //获取详情
  getDetail(){
    var customerCardId = this.data.customerCardId
    var data ={
      customerCardId: customerCardId
    }
    WXAPI.rechargeActivity(data,(res)=>{
      console.log('充值活动查询',res)
      const {result} = res
      this.setData({
        customerData: result,
      })
    })
  },
  //获取输入金额
  handleMoney(e){
    var money = e.detail.value
    console.log(money)
    if(money!=''){
      this.setData({
        show: true
      })
    }else{
      this.setData({
        show: false
      })
    }
    this.setData({
      money: money
    })
  },
  handlePay(){
    var money = this.data.money
    if(!money){
      wx.showToast({
        title: '请输入金额',
        icon: 'none'
      })
      return
    }else if(money<1 || money>10000){
      wx.showToast({
        title: '请输入1~10000元',
        icon: 'none'
      })
      return
    }
    this.getpay()
  },
  getpay(){
    const that = this
    var data = {
      customerCardId: this.data.customerCardId,
      payMoney: this.data.money
    }
    WXAPI.createRechargeOrder(data,(res)=>{
      console.log('支付',res)
      var data = res.result
      wx.requestPayment({
        'timeStamp': data.timeStamp,
        'nonceStr': data.nonceStr,
        'package': data.package,
        'signType': data.signType ,//MD5
        'paySign': data.paySign,
        'success': function (res) { 
            console.log('支付成功',res)
            console.log(res)
            if(res.errMsg == 'requestPayment:ok'){
              wx.showToast({
                title: '支付成功',
              })
              that.setData({
                orderId: data.orderId
              })
              that.getorderStatus()
              // setTimeout(() => {
              //   //支付页面信息
              //   wx.redirectTo({
              //     url: '/userPageA/pages/topupSuccess/topupSuccess?orderId='+data.orderId,
              //   })
              // }, 1500);
            }
          },
          'fail': function (res) { 
            wx.showToast({
              title: '支付失败',
              icon: 'none'
            })
            console.log('支付失败或取消支付',res)
          },
          'complete': function (res) { 
            console.log('支付完成',res)
            // wx.navigateBack({})
          }
      })
    })
  },
  //获取支付结果
  getorderStatus(){
    const that = this
    this.setData({
      timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
        that.data.countdown--;
        var data = {
          orderId: that.data.orderId,
        }
        WXAPI.getRechargePayResult(data, (res) =>{
          const data = res.result
          console.log("获取支付结果",res)
          if(data.isPay){
            wx.redirectTo({
              url: '/userPageA/pages/topupSuccess/topupSuccess?orderId='+that.data.orderId,
            })
            clearInterval(that.data.timer);
          }
        })
        that.setData({
          countdown: that.data.countdown,
        })
        if (that.data.countdown == 0) {
          console.log("1111111111")
          clearInterval(that.data.timer);
          that.setData({
            countdown: 60,
          })
        }
      }, 1000)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})